// xc_lshop/club/apply/apply.js
var act_bill = require('../model/distribution_manage')

var html2wxml = require('../../../html2wxml/html2wxml.js');
Page({
  mixins: [require('../../../xc/mixins/basicMixin')],
  /**
   * 页面的初始数据
   */
  data: {
    showapply: { ishow: false, actstatus: "waiting" },
    loading: true,
    /*表单验证是否通过数据*/
    vildfield: {
    },
    /* 表单提交的数据*/
    formdata: {},
    // 图片数据
    fileList: [],
    //弹框显示
    popshow: false,
    ischecked: false,
    txtmodel: null,
    region: []
  },
  xcloaddata: function (options, config) {
    //actclub_user

    var that = this
  },

  afterlogin: function (options, res, useinfo) {   
    var that= this
    var distribution = res.distribution;
    if (!distribution||distribution.status != 1 || distribution.condition.val != 1) {
       wx.redirectTo({
         url: res.basic.homeurl,
       }) 
      return ;
    }
    else
    {
      this.authop().then(item => {
        var memberinfo= useinfo.memberinfo       
        var updatemodel={};
        var temfield=["phone","name"] 
        temfield.forEach(item=>{          
          updatemodel["formdata."+item]=memberinfo[item]
        })
        that.setData(updatemodel)
        if(memberinfo.is_distribution=="1"){
          wx.redirectTo({
            url: '../manage/center',
          }) 
        } 
        else
        {
          act_bill.applyform().then(rform => {
         

            if (rform["status"] == 1) {
              that.setData({ txtmodel: rform["obj"] })
              if (rform.obj.hasagreement == "1") {
                html2wxml.html2wxml('article', rform.obj.agreementcontcont, that, 5);
                // html2wxml.html2wxml('articlecontents', rform.obj.contents, that, 5);
              }
              that.loadstatus(false)
            }
            else if (rform["status"] == 2) {
              var robj = rform["obj"]
    
              that.loadstatus(false)
              var widgetsapply = that.selectComponent("#widgetsapply")
              if (robj["islock"] == 1) {
                widgetsapply.showmodel("用户已经被锁定", "用户已经被锁定请联系管理员", 'warn', 'red')
                return;
              }
              if (robj["autarky"] == 1 || robj["applystatus"] == 2) {
                wx.redirectTo({
                  url: '../manage/center',
                })
                return;
              }
              else {
                if (robj["applystatus"] == 1) {
                  widgetsapply.showmodel("正在审核中...", "请耐心等待，1-3个工作日完成审核", 'waiting', that.data.themcolor.primarycolor)
                } else if (robj["applystatus"] == 3) {
                  widgetsapply.showmodel("您的审核未通过.", "请完善咨询", 'warn', 'red')
                }
                else if (robj["applystatus"] == 4) {
                  widgetsapply.showmodel("您的审核未通过.", "您的审核未通过.", 'warn', 'red')
                }
              }
    
            }
          })

        }       
      })
    }
 

 
   
 
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  // 表单提交
  formSubmit: function (e) {
    var valid = this.checkForm();
    var that = this
    if (valid) {
      act_bill.addapply(this.data.formdata).then(res => {
        if (res["status"] == 1) {
          that.setData({ "showapply.ishow": true })
        }
        else {
          wx.showToast({
            title: res["message"],
          })
        }

      })

    }

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },



  // 选择地址--点击选择
  selectArea: function () {
    this.selectComponent("#picker").setinitdata();
  },
  // 选择地址--点击确认
  chooseaddress: function (e) {
    var updatemodel = {}
    updatemodel["formdata.province"] = e.detail[0].name
    updatemodel["formdata.city"] = e.detail[1].name
    updatemodel["formdata.county"] = e.detail[2].name
    updatemodel["formdata.addrcode"] = [e.detail[0].id, e.detail[1].id, e.detail[2].id].join(",");
    this.setData(updatemodel)

  },
  // 获取手机号
  getPhoneNumber(e) {
    var that = this;
    var dataset = e.currentTarget.dataset;
    var encryptedData = e.detail.encryptedData;
    // if (!encryptedData) {
    //   return;
    // }
    var iv = e.detail.iv;
    var code = e.detail.code;

    var app = getApp();
    app.toolv2.getphone(encryptedData, iv,code).then(function (data) {
      if (data["code"] == "success") {
        var upatemodel = {};
        upatemodel["formdata.phone"] = data["obj"].phoneNumber;
        that.setData(upatemodel)
        // that.data.formdata["phone"] = data["obj"].phoneNumber
        that.checkForm("phone")
      }
    });
  },

  bindTimeChange(ev) {
    var dataset = ev.currentTarget.dataset
    var upatemodel = {}
    upatemodel["formdata." + dataset.field] = ev.detail.value
    upatemodel["vildfield." + dataset.field] = true
    this.setData(upatemodel)

  },
  // 同意
  onChange(event) {
    // var dataset = event.target.dataset
    this.setData({
      'vildfield.ischecked': event.detail,
      ischecked: event.detail

    });

    //this.checkForm(dataset.field)
  },
  //验证规则
  checkRule(field) {
    var updatemodel = {};
    if (field == "region") {
      updatemodel['vildfield.' + field] = (this.data.formdata[field] && this.data.formdata[field]['2']) ? true : false;
    } else if (field == "checked") {
      updatemodel['vildfield.' + field] = (this.data.formdata[field] && this.data.formdata[field] == 1) ? true : false;
    } else {
      updatemodel['vildfield.' + field] = this.data.formdata[field] ? true : false;
    }
    this.setData(updatemodel)
  },
  //验证表单
  checkForm: function (field) {

    if (field) {
      this.checkRule(field)
    } else {
      var valid = true;
      var vildfield = {};
      [ "name", "phone"].forEach(item => {
        if (this.data.formdata[item]) {
          vildfield[item] = true
        }
        else {
          vildfield[item] = false
          valid = false
        }
      })
      if (this.data.txtmodel["hasagreement"]) {
        if (!this.data.ischecked) {
          vildfield["ischecked"] = false
          valid = false
        }
        else {
          vildfield["ischecked"] = true
        }
      }
      this.setData({ vildfield })
      return valid;
    }

  },
  //监听input输入
  watchInput: function (e) {
    var dataset = e.target.dataset
    this.data.formdata[dataset.field] = e.detail
    this.checkForm(dataset.field)
  },
  //弹框显示隐藏
  onPopShowHide(e) {
    this.setData({
      popshow: !this.data.popshow
    });
  },
  //弹框同意事件
  agree(e) {
    this.setData({
      'vildfield.ischecked': true,
      ischecked: true,
      popshow: false
    });
  },
})